package com.ilibrary.utils;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 工具类
 * 
 * @author ZhongJiang
 *
 */
public class MyTools {
	
	/**
	 * @author JG
	 * 此方法专为此项目设计
	 * 获取传入日期X日后的日期
	 */
	public static Date getNextXDayDate(Date date, Integer x) {
		Calendar calendar = Calendar.getInstance();//日历对象
		calendar.setTime(date);//设置当前日期
		calendar.add(Calendar.DAY_OF_YEAR, x);//月份加1
		return calendar.getTime();
	}

	/**
	 * 获取两个日期之间间隔的天数
	 * @param borrowDt
	 * @param nowDate
	 * @return
	 */
	public static Integer getDayBetweenTwoDate(Date aDt, Date bDt) {
		//return (int) (Math.abs(aDt.getTime()-bDt.getTime())/(24*60*60*1000));
		return (int) (aDt.getTime()/(24*60*60*1000)-bDt.getTime()/(24*60*60*1000));
	}
	
	
	/**
	 * 
	 * @param dt
	 * @return yyyy-MM-dd HH:mm:ss
	 */	
	public static final String formatDt1(Date dt) {
		String style = "yyyy-MM-dd HH:mm:ss";
		DateFormat df = new SimpleDateFormat(style);
		return df.format(dt);
	}
	
	
	/**
	 * 
	 * @param dt
	 * @return yyyy-MM-dd
	 */	
	public static final String formatDt2(Date dt) {
		String style = "yyyy-MM-dd";
		DateFormat df = new SimpleDateFormat(style);
		return df.format(dt);
	}
	
	/**
	 * 
	 * @param dt
	 * @return yyyyMMdd
	 */
	public static final String formatDt3(Date dt) {
		String style = "yyyyMMdd";
		DateFormat df = new SimpleDateFormat(style);
		return df.format(dt);
	}
	
	/**
	 * 
	 * @param dt
	 * @return yyyyMMddHHmmss
	 */
	public static final String formatDt4(Date dt) {
		String style = "yyyyMMddHHmmss";
		DateFormat df = new SimpleDateFormat(style);
		return df.format(dt);
	}
	
	/**
	 * @function 将String转换成给定格式的Date
	 */
	public static final Date formateString2Date(String dtStr,String style) {
		DateFormat df = new SimpleDateFormat(style);
		Date date = null;
		try {
			date = (Date)df.parse(dtStr);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 转换中文乱码
	 * @param str
	 * @return
	 */
	public static final String convert2zHString(String tempkeywords) {
		byte[] tempByte = null;
		try {
			tempByte = tempkeywords.getBytes("ISO-8859-1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new String(tempByte);
	}
	
	
	public static final Integer convertItemCnt2PageCnt(Integer itemCnt, Integer eachPageCnt) {
		int pageCnt = 0;
		if(itemCnt%eachPageCnt > 0) {
			pageCnt = itemCnt/eachPageCnt + 1;
		}else {
			pageCnt = itemCnt/eachPageCnt;
		}
		return pageCnt;
	}
	
	public static final String countAVG2OnePoint(Double total,Integer cnt) {
		DecimalFormat df = new DecimalFormat("#.0");
		Double avg = total/cnt;
		return df.format(avg).toString();
	}
	
}













